package com.mall.admin.controller;

import cn.hutool.core.convert.Convert;
import com.mall.admin.service.UmsAdminService;
import com.mall.commons.utils.R;
import com.mall.domain.UmsAdmin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Slf4j
public class UmsAdminController {
    @Autowired
    private UmsAdminService umsAdminService;
    //通过map接收登录账号跟密码
    @PostMapping("/login")
    public R login(@RequestBody Map<String,String> map){
        log.info("map:{}",map);
    return umsAdminService.login(map);
    }
    @GetMapping("/info")
    public R info(){
        return umsAdminService.getUserInfo();
    }
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        return umsAdminService.deleteUser(id);
    }
    @GetMapping("list")
    public R listAll(Integer pageNum,Integer pageSize,String keyword){
       return umsAdminService.listAll(pageNum, pageSize, keyword);
    }
    @PostMapping("/logout")
    public R logout(@RequestHeader("Authorization")String token){
        return umsAdminService.logout(token);
    }
    @GetMapping("refreshToken")
    public R refreshToken(@RequestHeader("Authorization")String token){
        return umsAdminService.refreshToken(token);
    }
    @PostMapping("/register")
    public R register(@RequestBody UmsAdmin umsAdmin){
        return umsAdminService.register(umsAdmin);
    }
    @PostMapping("/role/update")
    public R updateRole(@RequestBody Map<String,Object> map){
        return umsAdminService.updateRole(map);
    }
    @GetMapping("role/{adminId}")
    public R getRole(@PathVariable Integer adminId){
        return umsAdminService.getRole(Convert.toLong(adminId));
    }
    @GetMapping("update/{id}")
    public R updateRole(@PathVariable Integer id,@RequestBody UmsAdmin umsAdmin){
        umsAdmin.setId(Convert.toLong(id));
        return umsAdminService.updateAdmin(umsAdmin);
    }
    @PostMapping("updatePassword")
    public R updatePassword(@RequestBody Map<String,String> map){
       return umsAdminService.updatepassword(map);
    }
    @PostMapping("/updateStatus/{id}")
    public R updateStatus(Integer status,@PathVariable Integer id){
        return umsAdminService.updatestatus(status,id);
    }
    @GetMapping("/{id}")
    public R get(@PathVariable Integer id){
        return umsAdminService.get(id);
    }

}
